package com.jrx.anytxn.param.service.account.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.account.SubjectAccountConfigReq;
import com.jrx.anytxn.param.dto.account.SubjectAccountConfigRes;
import com.jrx.anytxn.param.entity.GaSubjectAccountConfig;
import com.jrx.anytxn.param.mapper.ext.ExtGaSubjectAccountConfigMapper;
import com.jrx.anytxn.param.service.account.ISubjectAccountConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 科目内部户配置service实现
 *
 * @author yf
 * date 2019/12/24 0024
 * @version V1.0
 **/
@Service
@CacheConfig(cacheNames = "SUBJECT_ACCOUNT")
public class SubjectAccountConfigServiceImpl implements ISubjectAccountConfigService {

    private static final Logger logger = LoggerFactory.getLogger(SubjectAccountConfigServiceImpl.class);

    @Autowired
    private ExtGaSubjectAccountConfigMapper extGaSubjectAccountConfigMapper;

    @Autowired
    private ISegmentService segmentService;

    @Override
    public TxnPage<SubjectAccountConfigRes> getAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<SubjectAccountConfigRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<GaSubjectAccountConfig> gaSubjectAccountConfigList = extGaSubjectAccountConfigMapper.selectByTenId(tenantId);
        if (gaSubjectAccountConfigList.isEmpty()) {
            logger.error("查询的科目内部户配置表列表为空");
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<SubjectAccountConfigRes> res = BeanMapping.copyList(gaSubjectAccountConfigList, SubjectAccountConfigRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public SubjectAccountConfigRes getByTenIdIdAndSubjectId(String subjectId, String tenantId, String productId) throws TxnException {
        if (StringUtils.isBlank(subjectId) || StringUtils.isBlank(tenantId)||StringUtils.isBlank(productId)) {
            logger.error("科目ID或租户ID或产品ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        GaSubjectAccountConfig gaSubjectAccountConfig = extGaSubjectAccountConfigMapper.selectBySubjectIdAndTenIdAndProId(tenantId, subjectId,productId);
        return BeanMapping.copy(gaSubjectAccountConfig, SubjectAccountConfigRes.class);
    }

    @Cacheable(key = "'subject_account_'+#tenantId+'_'+#subjectId+'_'+#productId")
    @Override
    public GaSubjectAccountConfig findBySubjectIdAndProductId(String tenantId, String subjectId, String productId) {
        GaSubjectAccountConfig gaSubjectAccountConfig = extGaSubjectAccountConfigMapper.selectByTenantIdAndSubjectIdAndProdIdAndStatus(tenantId, subjectId, productId, Constant.STATUS_1);
        return gaSubjectAccountConfig;
    }

    @Override
    public SubjectAccountConfigRes addSubjectAccountConfig(SubjectAccountConfigReq subjectAccountConfigReq) throws TxnException {
        try {
            subjectAccountConfigReq.setId(segmentService.getId(Constant.GA_SUBJECT_ACCOUNT_CONFIG));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        GaSubjectAccountConfig gaSubjectAccountConfig = BeanMapping.copy(subjectAccountConfigReq, GaSubjectAccountConfig.class);
        /*根据索引查看是否存在此信息*/
        GaSubjectAccountConfig table = extGaSubjectAccountConfigMapper.selectBySubjectIdAndTenIdAndProId(subjectAccountConfigReq.getTenantId(), subjectAccountConfigReq.getSubjectId(),subjectAccountConfigReq.getProductId());
        if (table != null) {
            logger.error("增加的科目内部户配置信息已存在");
            throw new TxnException(TxnRespCode.DATA_EXISTS, TxnRespCode.DATA_EXISTS.getMsg());
        }
        gaSubjectAccountConfig.setRecordVersionNumber(Constant.VERSION_NUMBER);

        extGaSubjectAccountConfigMapper.insertSelective(gaSubjectAccountConfig);
        return BeanMapping.copy(gaSubjectAccountConfig, SubjectAccountConfigRes.class);
    }

    @CacheEvict(key = "'subject_account_'+#subjectAccountConfigReq.tenantId+'_'+#subjectAccountConfigReq.subjectId+'_'+#subjectAccountConfigReq.productId")
    @Override
    public SubjectAccountConfigRes modifySubjectAccountConfig(SubjectAccountConfigReq subjectAccountConfigReq){
        GaSubjectAccountConfig gaSubjectAccountConfig = BeanMapping.copy(subjectAccountConfigReq, GaSubjectAccountConfig.class);
        extGaSubjectAccountConfigMapper.updateByTenIdAndSubjectIdAndProId(gaSubjectAccountConfig);
        return BeanMapping.copy(gaSubjectAccountConfig, SubjectAccountConfigRes.class);
    }

    @CacheEvict(key = "'subject_account_'+#tenantId+'_'+#subjectId+'_'+#productId")
    @Override
    public Boolean removeBySubIdAndProIdAndTenId(String tenantId, String subjectId, String productId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(subjectId)||StringUtils.isBlank(productId)) {
            logger.error("科目表主键ID或租户ID或产品ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int a = extGaSubjectAccountConfigMapper.deleteByTenIdAndSubjectIdAndProId(tenantId, subjectId,productId);
        return a > 0;
    }
}
